package ru.ifmo.swing.genetics.adapters;

import org.uncommons.maths.number.NumberGenerator;
import org.uncommons.watchmaker.framework.CandidateFactory;
import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory;
import ru.ifmo.common.Source;
import ru.ifmo.genetics.factory.string.DistributionBasedStringFactory;
import ru.ifmo.swing.ProgressStartListener;

import java.util.List;
import java.util.Random;

/**
 * @author avhaliullin
 */
public class AutomatonBasedStringFactoryAdapter extends AbstractCandidateFactory<String> implements ProgressStartListener {
    private final NumberGenerator<Integer> maxLength;
    private final Source<List<Character>> alphabetProvider;
    private final NumberGenerator<Integer> symbolNumberGenerator;
    private CandidateFactory<String> factory;

    public AutomatonBasedStringFactoryAdapter(
            NumberGenerator<Integer> maxLength, Source<List<Character>> alphabetProvider, NumberGenerator<Integer> symbolNumberGenerator) {
        this.alphabetProvider = alphabetProvider;
        this.maxLength = maxLength;
        this.symbolNumberGenerator = symbolNumberGenerator;
    }

    public String generateRandomCandidate(Random rng) {
        return factory.generateRandomCandidate(rng);
    }

    public void progressStarted() {
        factory = new DistributionBasedStringFactory(maxLength, alphabetProvider.create(), symbolNumberGenerator);
    }
}
